import { UserRole } from '@/types/enum';
import { ls, setTabBar, switchTab } from '@/utils';
import { switchMeberRoles } from '@/api/common';

const app = {
	state: () => ({
		/* 基本色 */
		PRIMARY_COLOR: '#3E9DBB',
		/* 页面加载 */
		PAGE_LOAD: false,
		/* 用户角色 */
		USER_ROLE: ls.get('USER_ROLE') || UserRole.Master,
		// app存放地址
		APP_PATH: '',
		// 定位
		location: ls.get('location') || {
			cityName: '广州',
		},
	}),
	mutations: {
		/* 设置页面加载效果 */
		SET_PAGE_LOAD(state: any, PAGE_LOAD: boolean) {
			state.PAGE_LOAD = PAGE_LOAD;
		},
		/* 设置用户角色 */
		SET_USER_ROLE(state: any, USER_ROLE: UserRole) {
			state.USER_ROLE = USER_ROLE;
			ls.set('USER_ROLE', USER_ROLE);
		},
		/* 设置app存放路径 */
		SET_APP_PATH(state: any, path: string) {
			state.APP_PATH = path;
		},
		/* 设置定位 */
		SET_LOCATION(state: any, cityName: string) {
			state.location.cityName = cityName;
			ls.set('location', state.location);
		},
	},
	actions: {
		// 设置用户角色
		SET_USER_ROLE(context, USER_ROLE: UserRole) {
			context.commit('SET_PAGE_LOAD', true);
			switchMeberRoles(USER_ROLE).then(async res => {
				context.dispatch('SET_MASTER_INFO');
				context.dispatch('SET_SERVICE_INFO');
				context.dispatch('SET_ADDRESS_LIST');
				setTimeout(() => {
					context.commit('SET_PAGE_LOAD', false);
					context.commit('SET_USER_ROLE', USER_ROLE);
					setTabBar(USER_ROLE, context.rootState);
					// 跳转到首页
					switchTab('/pages/index/index');
				}, 1500)
			});
		}
	},
}

export default app;